package com.mybatisflex.example.service.impl;

import com.mybatisflex.core.query.If;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.example.mapper.taos.TemperatureMapper;
import com.mybatisflex.example.pojo.domain.table.TemperatureTableDef;
import com.mybatisflex.example.pojo.domain.taos.Temperature;
import com.mybatisflex.example.service.TemperatureService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 气温 服务层实现。
 *
 * @author tanyunpeng
 * @since 2024-02-28
 */
@Service
public class TemperatureServiceImpl extends ServiceImpl<TemperatureMapper, Temperature> implements TemperatureService {


    @Override
    public boolean saveOne(Temperature temperature) {
        return this.mapper.insertOne(temperature) > 0;
    }

    @Override
    public List<Temperature> query(Integer districtCode, String districtName) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(TemperatureTableDef.T_TEMPERATURE.DEFAULT_COLUMNS)
                .from(TemperatureTableDef.T_TEMPERATURE)
                .where(TemperatureTableDef.T_TEMPERATURE.DISTRICT_CODE.eq(districtCode, If::notNull))
                .and(TemperatureTableDef.T_TEMPERATURE.DISTRICT_NAME.likeLeft(districtName, If::hasText))
                .orderBy(TemperatureTableDef.T_TEMPERATURE.TS.desc());
        return mapper.selectListByQuery(queryWrapper);
    }
}